package com.standstill.controller;

import com.netflix.discovery.converters.Auto;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import com.standstill.controller.reqobj.DemoObject;
import com.standstill.service.InnerService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Guo Qiao on 2017/11/27.
 */

@RefreshScope
@RestController
@RequestMapping("srv1/demo")
public class DemoController {

    /*@Autowired
    private RestTemplate restTemplate;*/

    @Autowired
    private InnerService innerService;

    @Value("${prop.server01.test}")
    private String configProp;

    @RequestMapping("hw")
    public Object helloWorld(){
        return "Hello World!";
    }

    @RequestMapping("urlparam/{param}")
    public Object urlParam(@PathVariable String param){
        return "Hello " + param;
    }

    @RequestMapping("reqparam")
    public Object requestParam(@RequestParam(required = false) String param){
        return "Hello " + (param == null ? configProp : param);
    }

    @RequestMapping("reqobj")
    public Object requestObject(@RequestBody DemoObject demoObject){
        return JSONObject.fromObject(demoObject);
    }

    private final String INNER_SERVICE_HOST_NAME = "SERVICE-INNER";//SERVICE-INNER


    @Autowired
    private DiscoveryClient discoveryClient;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping("callinner")
    @HystrixCommand(fallbackMethod = "callExceptionResult")
    public String callInner(){
        return innerService.getInfo();//restTemplate.getForEntity("http://" + INNER_SERVICE_HOST_NAME + "/srv/demo/getinfo" , String.class).getBody();
    }

    public String callExceptionResult(){
        return "call error, return default";
    }

}
